package cn.ryh.pay.controller;

import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.wx.api.WxPayConfigStorage;
import com.egzosn.pay.wx.api.WxPayService;
import com.egzosn.pay.wx.bean.WxTransactionType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;
import java.util.UUID;

/**
 * @author Raoyh
 * @Description
 * @create 2024-06-19 16:37
 */

@RestController
public class PayController2 {

    private PayService service = null;

    /**
     * ("MchId", "1226460602");
     * ("AppId", "wxcf19d5db319405da");
     * ("AppSecret", "62e84c8171766a1216d36dfc0b76379b");
     * ("AppKey", "f506635d4f9eebe5b572d190fcef0048");
     */
    @PostConstruct
    public void init() {
        WxPayConfigStorage wxPayConfigStorage = new WxPayConfigStorage();
        wxPayConfigStorage.setMchId("1226460602");
        wxPayConfigStorage.setAppid("wxcf19d5db319405da");
        // wxPayConfigStorage.setKeyPublic("转账公钥，转账时必填");
        wxPayConfigStorage.setSecretKey("2e84c8171766a1216d36dfc0b76379b");
        wxPayConfigStorage.setNotifyUrl("https://www.raoyuehua.online:18889/api/pay/testNativeNotify");
        wxPayConfigStorage.setReturnUrl("https://www.raoyuehua.online:18889/api/pay/testNativeNotify");
        wxPayConfigStorage.setSignType("MD5");
        wxPayConfigStorage.setInputCharset("utf-8");

        service = new WxPayService(wxPayConfigStorage);


    }

    /**
     * 跳到支付页面
     * 针对实时支付,即时付款
     *
     * @return
     */
    @RequestMapping(value = "toPay.html", produces = "text/html;charset=UTF-8")
    public String toPay() {

        PayOrder order = new PayOrder("订单title", "摘要", new BigDecimal(0.01), UUID.randomUUID().toString().replace("-", ""));
        //网页支付
        order.setTransactionType(WxTransactionType.MWEB);
        //获取支付订单信息
        Map orderInfo = service.orderInfo(order);
        //组装成html表单信息
        return service.buildRequest(orderInfo, MethodType.POST);
    }


    /**
     * 获取二维码图像
     * 二维码支付
     *
     * @return
     */
    @RequestMapping(value = "toQrPay.jpg", produces = "image/jpeg;charset=UTF-8")
    public byte[] toWxQrPay() throws IOException {
        PayOrder order = new PayOrder("订单title", "摘要", new BigDecimal(0.01), UUID.randomUUID().toString().replace("-", ""));
        //扫码付
        order.setTransactionType(WxTransactionType.NATIVE);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(service.genQrPay(order), "JPEG", baos);
        return baos.toByteArray();
    }

    /**
     * 支付回调地址
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "payBack.json")
    public String payBack(HttpServletRequest request) throws IOException {

        //获取支付方返回的对应参数
        Map<String, Object> params = service.getParameter2Map(request.getParameterMap(), request.getInputStream());
        if (null == params) {
            return service.getPayOutMessage("fail", "失败").toMessage();
        }

        //校验
        if (service.verify(params)) {
            //这里处理业务逻辑
            return service.getPayOutMessage("success", "成功").toMessage();
        }

        return service.getPayOutMessage("fail", "失败").toMessage();
    }
}
